#include "ScnsEspAtUart.c.h"

#if defined(SCNS_ESP_AT_UART_ENABLE)&&SCNS_ESP_AT_UART_ENABLE==1

#include "usart.h"
#include "ScnsSmctpExtLogger.h"
#include "ScnsMenu.h"
#include "rtc.h"
#include "ScnsBspEspAtUart.h"
#include "ScnsPcf8574.h"

#if defined(__has_include)
#if __has_include ("hxMqttConfig.h")

#include "hxMqttConfig.h"
#include "hxMqtt.h"

#endif
#endif

const ScnsBspEspAtUartConfig scnsBspEspAtUartConfig[SCNS_ESP_AT_UART_MAX]={
    {
        .station={
            .ssid="DoYouLikeJuruoyun",
            //.ssid="doYouLikeJuruoyun",
            .password="ILikeJuruoyun",
        },
        .tcpClient={
#if defined(HX_MQTT_SERVER)&&defined(HX_MQTT_PORT)
            .protocol="SSL",
            .ip=HX_MQTT_SERVER,
            .port=HX_MQTT_PORT,
#else
            .ip="192.168.137.1",
            .port=1000,
#endif
        },
    },
};

void scnsBspEspAtUartSetEn(ScnsEspAtUartEnum espN,uint8 dat)
{
    scnsPcf8574WriteBit(SCNS_PCF8574_1,6,dat);
}

void scnsBspEspAtUartSetRst(ScnsEspAtUartEnum espN,uint8 dat)
{
    scnsPcf8574WriteBit(SCNS_PCF8574_1,7,dat);
}

void scnsBspEspAtUartSend(ScnsEspAtUartEnum espN,const uint8*buf,uint16*len)
{
    HAL_UART_Transmit_DMA(&huart5,(uint8*)buf,*len);
}

void scnsBspEspAtUartReceiveIdleDmaStart(ScnsEspAtUartEnum espN,uint8*buf,uint32 len)
{
    HAL_UARTEx_ReceiveToIdle_DMA(&huart5,buf,len);
}

void scnsBspEspAtUartReceiveIdleDmaStop(ScnsEspAtUartEnum espN,uint8*buf,uint32 len)
{
    HAL_UART_AbortReceive(&huart5);
}

void scnsBspEspAtUartWaiting(ScnsEspAtUartEnum espN)
{
#if defined(SCNS_LCD_ENABLE)&&SCNS_LCD_ENABLE==1&&defined(SCNS_SMCTP_ENABLE)&&SCNS_SMCTP_ENABLE==1&&defined(SCNS_SMCTP_EXT_LOGGER_ENABLE)&&SCNS_SMCTP_EXT_LOGGER_ENABLE==1
    {
        const Point a=scnsLcdGetPhysicalPlu(1);
        const Point b=scnsLcdGetPhysicalPrd(1);
        scnsSmctpExtLoggerDisplay(SCNS_LCD_LAYER_AUTO,(Point){a.x,b.y-16*10},16,0,480,1,6,&scnsLcdFontConsolas10,SCNS_LCD_COLOR_RED,SCNS_LCD_COLOR_BACKGROUND,0);
    }
#endif
#if defined(SCNS_MENU_ENABLE)&&SCNS_MENU_ENABLE==1
    scnsMenuFresh();
#endif
}

void scnsBspEspAtUartStationConnected(ScnsEspAtUartEnum espN,uint8*ip)
{
    hxPrintfFL("wifi connected %s\n",ip);
}

void scnsBspEspAtUartPathThroughReceive(ScnsEspAtUartEnum espN,const uint8*buf,uint16 len)
{
    hxMqttRecv(buf,len);
}

void scnsBspEspAtUartGotSNTP(ScnsEspAtUartEnum espN,uint16 year,uint8 month,uint8 day,uint8 hour,uint8 minute,uint8 second)
{
    LL_RTC_TimeTypeDef rtcTime={.Hours=hour,.Minutes=minute,.Seconds=second};
    LL_RTC_DateTypeDef rtcDate={.Day=day,.Month=month,.Year=year-2000};
    LL_RTC_TIME_Init(RTC,LL_RTC_FORMAT_BIN,&rtcTime);
    LL_RTC_DATE_Init(RTC,LL_RTC_FORMAT_BIN,&rtcDate);
    (void)rtcTime;
    (void)rtcDate;
}

#endif
